# @Time: 2022/5/10
# @Dev: TJ
"""
    pip install Faker~=8.10.0
"""

import string

from faker import Faker
from faker.generator import Generator
from faker.providers.user_agent import Provider


class SubFakerUA(Provider):
    """ 伪UA """
    PC = 'pc'  # pc端
    Mobile = 'mobile'  # 移动设备

    config = dict()
    config['locale'] = 'en_US'
    config['use_weighting'] = True

    user_agents = ('chrome', 'internet_explorer')

    # iOS版本
    ios_versions = ('10.3.3', '10.3.4', '12.4.8', '14.2', '14.2.1')
    # 安卓版本
    android_versions = (
        '5.0', '5.0.1', '5.0.2', '5.1', '5.1.1', '6.0', '6.0.1',
        '7.0', '7.1', '7.1.1', '7.1.2', '8.0.0', '8.1.0', '9', '10', '11')

    def __init__(self):
        super().__init__(generator=Generator(**self.config))

    def user_agent(self):
        """Generate a random web browser user agent string."""
        name = self.random_element(self.user_agents)
        return getattr(self, name)()

    def chrome(self, version_from=81, version_to=98,
               build_from=800, build_to=899, platform: str = PC):
        """Generate a Chrome web browser user agent string."""
        saf = f'{self.generator.random.randint(531, 536)}.{self.generator.random.randint(0, 2)}'
        bld = self.lexify(self.numerify('##?###'), string.ascii_uppercase)

        tmplt = '({0}) AppleWebKit/{1} (KHTML, like Gecko)' \
                ' Chrome/{2}.0.{3}.0 Safari/{4}'
        tmplt_ios = '({0}) AppleWebKit/{1} (KHTML, like Gecko)' \
                    ' CriOS/{2}.0.{3}.0 Mobile/{4} Safari/{1}'

        if platform == self.Mobile:
            platforms = (
                # 安卓
                tmplt.format(
                    'Linux; {}'.format(self.android_platform_token()),
                    saf,
                    self.generator.random.randint(version_from, version_to),
                    self.generator.random.randint(build_from, build_to),
                    saf),
                # iOS
                tmplt_ios.format(
                    self.ios_platform_token(),
                    saf,
                    self.generator.random.randint(version_from, version_to),
                    self.generator.random.randint(build_from, build_to),
                    bld)
            )
        else:
            platforms = (
                # win
                tmplt.format(
                    self.windows_platform_token(),
                    saf,
                    self.generator.random.randint(version_from, version_to),
                    self.generator.random.randint(build_from, build_to),
                    saf),
                # mac
                tmplt.format(
                    self.mac_platform_token(),
                    saf,
                    self.generator.random.randint(version_from, version_to),
                    self.generator.random.randint(build_from, build_to),
                    saf)
            )

        return 'Mozilla/5.0 ' + self.random_element(platforms)

    def firefox(self):
        return Faker().firefox()

    def internet_explorer(self):
        """Generate an IE web browser user agent string."""
        return (f'Mozilla/5.0 (compatible; MSIE {self.generator.random.randint(5, 9)}.0; '
                f'{self.windows_platform_token()}; '
                f'Trident/{self.generator.random.randint(3, 5)}.{self.generator.random.randint(0, 1)})')


if __name__ == '__main__':
    for _ in range(10):
        print('chrome pc:', SubFakerUA().chrome())
        print('chrome mobile:', SubFakerUA().chrome(platform=SubFakerUA.Mobile))
        print('IE:', SubFakerUA().internet_explorer())
        print('random:', SubFakerUA().user_agent())
        print('firefox:', SubFakerUA().firefox())
